import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login/Login'
import Reg from '@/views/Reg/Reg'
import Main from '@/views/Main/Main'
import store from '@/store'
import Home from '@/views/Menu/Home/Home'
import UserInfo from '@/views/Menu/User/UserInfo'
import UserAvatar from '@/views/Menu/User/UserAvatar'
import UserPwd from '@/views/Menu/User/UserPwd'
import ArtCate from '@/views/Menu/article/ArtCate'
import ArtList from '@/views/Menu/article/ArtList'

Vue.use(VueRouter)

const routes = [
  // 第一次跳转页面时默认显示注册页
  // { path: '/', redirect: '/reg' },
  { path: '/reg', component: Reg },
  { path: '/login', component: Login },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      { path: 'home', component: Home },
      { path: 'user-info', component: UserInfo },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 添加全局守卫
router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '/reg']
  const token = store.state.user.token
  if (!token && !whiteList.includes(to.path)) {
    next('/login')
    Vue.prototype.$message.error('您还未登录哦亲~~~')

    // 提醒用户
    // alert('~~~~')
  } else {
    next()
  }
})

export default router
